package com.atguigu.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.atguigu.dao.TravelItemDao;
import com.atguigu.pojo.TravelItem;
import com.atguigu.service.TravelItemService;
import com.atguigu.utils.DownLoadUtils;
import com.atguigu.vo.PageResult;
import com.atguigu.vo.QueryPageBean;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author zhengkaiyuan
 * @version 1.0
 * @create 2021-06-19 15:52
 */
@Transactional
@Service(interfaceClass = TravelItemService.class)
public class TravelItemServiceImpl implements TravelItemService {
    @Autowired
    private TravelItemDao travelItemDao;

    @Override
    public void add(TravelItem travelItem) {
        travelItemDao.add(travelItem);
    }

    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {
        //开启分页
        PageHelper.startPage(currentPage, pageSize);
        Page<TravelItem> page = travelItemDao.findPage(queryString);
        List<TravelItem> result = page.getResult();
        System.out.println(result);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public void deleteById(Integer id) {
        //判断当前自由行是否与团游进行绑定
        long count = travelItemDao.findCountByTravelItemItemId(id);
        if (count > 0) {
            throw new RuntimeException("删除自由行失败，原因：存在关联数据" + count + "条");
        }
        travelItemDao.deleteById(id);
    }

    @Override
    public TravelItem findById(Integer id) {
        TravelItem travelItem = travelItemDao.findById(id);
        return travelItem;
    }

    @Override
    public void edit(TravelItem travelItem) {
        travelItemDao.edit(travelItem);
    }

    @Override
    public List<TravelItem> findALL() {
        return travelItemDao.findAll();
    }


    /**
     * @param filePath    上传文件的目录
     * @param queryString 查询数据的条件
     */
    @Override
    public String downLoadFile(String filePath, String queryString) throws Exception {
        if ("null".equals(queryString)) {
            queryString = "";
        }
        //查询出当前数据
        List<TravelItem> result = travelItemDao.findByCondition(queryString);
        System.out.println("result = " + result);
        String excelName = "TravelItem";

        String excelToName = DownLoadUtils.createExcelToName(excelName, result, filePath);
        return excelToName;
    }

}



